package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JOptionPane;

import Custom.Encrypt;
import DataLayer.DaoConnection;
import DataLayer.DaoUser;
import Model.User;
import View.PanelDashboard;
import View.MainFrame;
import View.PanelHolder;
import View.PanelLogin;

public class Inloggen extends DaoConnection implements ActionListener, KeyListener {
	
	private DaoUser user = DaoUser.getInstance();
	private PanelLogin login;
	private User g = new User();
	private Encrypt encrypt = new Encrypt();
	private DaoUser dao = DaoUser.getInstance();
	
	public Inloggen(PanelLogin login) {
		this.login = login;
	}
	
	
public void keyPressed(KeyEvent e) {
		
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			String emailAdres = login.getEmailField().getText();
			@SuppressWarnings("deprecation")
			String password = login.getPasswordField().getText();
			try {
				password = encrypt.crypt(password);
			} catch (Exception e2) {
				e2.printStackTrace();
				JOptionPane
				.showMessageDialog(null,
						"U heeft geen wachtwoord ingegeven",
						null, JOptionPane.ERROR_MESSAGE);
			}
		
			if(emailAdres.equals("")){
				JOptionPane
				.showMessageDialog(null,
						"U heeft geen e-mailadres ingevuld",
						null, JOptionPane.ERROR_MESSAGE);
			}
			
			if(!emailAdres.equals("") && !password.equals(0)){
				
				g.setemailAdres(emailAdres);
				g.setpassword(password);
				dao.loginCheck(g);
				System.out.println("aah Yes");
				if(emailAdres.equals(user.dbEmailAdres) 
					&& password.equals(user.dbPassword)){
					
					g.setpassword(password);
					g.setemailAdres(emailAdres);
					
					MainFrame mf = MainFrame.getInstance();
					PanelHolder hold = PanelHolder.getInstance();
					
					mf.remove(PanelLogin.getInstance());
					PanelLogin.selfDestroy();
					mf.add(hold);
					hold.add(PanelDashboard.getInstance());
					mf.repaint();
					mf.validate();	
					
					}else{
						JOptionPane
						.showMessageDialog(null,
								"Het e-mailadres of wachtwoord is onjuist",
								null, JOptionPane.ERROR_MESSAGE);
				}	
				
					}
		}
	}

	public void actionPerformed(ActionEvent e) {

		
		String emailAdres = login.getEmailField().getText();
		@SuppressWarnings("deprecation")
		String password = login.getPasswordField().getText();
		try {
			password = encrypt.crypt(password);
		} catch (Exception e2) {
			e2.printStackTrace();
			JOptionPane
			.showMessageDialog(null,
					"U heeft geen wachtwoord ingegeven",
					null, JOptionPane.ERROR_MESSAGE);
		}
	
		if(emailAdres.equals("")){
			JOptionPane
			.showMessageDialog(null,
					"U heeft geen e-mailadres ingevuld",
					null, JOptionPane.ERROR_MESSAGE);
		}
		
		if(!emailAdres.equals("") && !password.equals(0)){
			
			g.setemailAdres(emailAdres);
			g.setpassword(password);
			dao.loginCheck(g);
			System.out.println("aah Yes");
			if(emailAdres.equals(user.dbEmailAdres) 
				&& password.equals(user.dbPassword)){
				
				g.setpassword(password);
				g.setemailAdres(emailAdres);
				
				MainFrame mf = MainFrame.getInstance();
				PanelHolder hold = PanelHolder.getInstance();
				
				mf.remove(PanelLogin.getInstance());
				PanelLogin.selfDestroy();
				mf.add(hold);
				hold.add(PanelDashboard.getInstance());
				mf.repaint();
				mf.validate();	
				
				}else{
					JOptionPane
					.showMessageDialog(null,
							"Het e-mailadres of wachtwoord is onjuist",
							null, JOptionPane.ERROR_MESSAGE);
			}	
			
				}
		}


	public void keyTyped(KeyEvent e) {
		
	}

	public void keyReleased(KeyEvent e) {
		
	}
	}